﻿Option Explicit On
Imports System.Runtime.InteropServices
Imports System
Imports System.IO

Public Class frmMirkvartir
    Private cat As Object
    Private params As New ArrayList
    Private is_run As Boolean
    Private Const SITE_URL As String = "http://www.mirkvartir.ru/"
    Private adverts() As AdvertType
    Private cur_adv As Integer
    Private tmpl(9) As Object
    Public Const ADV_NAME As String = "mirkvartir"
    Public file_name As String
    Public catalog_name As String
    Private h1 As String
    Private oHttpXML As MSXML2.XMLHTTP


    Private Sub frmMirkvartir_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cat = {"Комнаты/", ""}
        params.Add("")
        params.Add("&roomsFrom=1&roomsTo=1")
        params.Add("&roomsFrom=2&roomsTo=2")
        params.Add("&roomsFrom=3&roomsTo=3")
        params.Add("&roomsFrom=4&roomsTo=4")
        params.Add("&roomsFrom=5&roomsTo=5")
        params.Add("&roomsFrom=6&roomsTo=6")
        cboCatalog.SelectedIndex = 0
        grbGrabber.Visible = False
        web.ScriptErrorsSuppressed = True
        lstConsole.Left = 0
        lstConsole.Dock = DockStyle.Bottom
    End Sub

    Private Sub frmMirkvartir_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        lstConsole.Height = Me.Height - 100
        web.Height = Me.Height - 100
    End Sub


    Private Sub cboCatalog_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCatalog.SelectedIndexChanged
        Dim n As Integer
        cboKomnat.Items.Clear()
        cboKomnat.Items.Add("Все")
        If cboCatalog.SelectedIndex = 1 Then
            For n = 1 To params.Count - 1
                cboKomnat.Items.Add(n & " комнатные")
            Next
        End If
        cboKomnat.SelectedIndex = 0
    End Sub



    Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
        Dim params_txt As String
        Dim url As String, catalog As String
        Dim regexp As New System.Text.RegularExpressions.Regex("")
        Dim match As System.Text.RegularExpressions.Match
        Dim p As Integer, max_page As Integer
        Dim response As Object

        btnRun.Enabled = False

        'Подготавливаем URL
        catalog = cat(cboCatalog.SelectedIndex)
        params_txt = params(cboKomnat.SelectedIndex)
        url = SITE_URL & "Хабаровский+край/Комсомольск-на-Амуре/" & catalog & "?pp=100" & params_txt

        'Получить первую страницу
        response = GetHTTPResponse(oHttpXML, url)
        If VarType(response) = VariantType.Long Then
            myConsole(lstConsole, "Сервер недоступен. Код возврата " & response) : Exit Sub
        End If

        'Найдем кол-во страниц
        regexp = New System.Text.RegularExpressions.Regex("Найдено <b>(.*)</b>")
        match = regexp.Match(response)
        If match.Success = False Then
            myConsole(lstConsole, "Этот раздел пустой")
            Exit Sub
        End If
        max_page = CInt(regexp.Matches(response).Item(0).Groups(1).Value.Replace(" ", ""))
        max_page = max_page \ 100 + 1
        myConsole(lstConsole, "В разделе найдено " & max_page & " страниц")

        'В цикле проходим по всем страницам
        cur_adv = 0
        ReDim adverts(0)
        For p = 1 To max_page
            myConsole(lstConsole, "Загружаем " & p & "-ю страницу")
            'Первую страницу мы уже получили
            If p <> 1 Then
                response = GetHTTPResponse(oHttpXML, url & "&p=" & p)
                If VarType(response) = VariantType.Long Then
                    myConsole(lstConsole, "Сервер недоступен. Код возврата " & response) : Exit Sub
                End If
            End If

            'Получаем со страницы все объявления
            regexp = New System.Text.RegularExpressions.Regex("<a href=""/(\d{4,12})/"" class=""left photo"">")
            For Each match In regexp.Matches(response)
                ReDim Preserve adverts(cur_adv)
                adverts(cur_adv).url = match.Groups(1).Value & "/"
                adverts(cur_adv).advid = match.Groups(1).Value
                cur_adv = cur_adv + 1
            Next
        Next

        'Запускаем граббер
        h1 = "Мир квартир - " & cboCatalog.Text & " " & cboKomnat.Text
        timTick.Interval = 3000
        timTick.Start()
        grbGrabber.Visible = True
        cur_adv = 0
        txtCur.Text = 0
        txtMax.Text = adverts.Count
        myConsole(lstConsole, "Найдено " & adverts.Count & " объявлений")
        lstConsole.Items.Add("")
    End Sub



    Private Sub timTick_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timTick.Tick
        Dim regexp As New System.Text.RegularExpressions.Regex("")
        Dim phone As String
        Dim response As String
        Dim tmpS As String, tmpS2 As String, tmpO As Object
        Dim n As Integer

        timTick.Stop()
        txtCur.Text = cur_adv + 1
        myConsole(lstConsole, "Загружаем " & (cur_adv + 1) & "-е объявление")

        With adverts(cur_adv)
            'Проверим если объявление старое, то не загружаем его
            If chkOnlyNew.Checked = True Then
                If DBAdverts_isOldAdvert(ADV_NAME, .advid) = True Then
                    myConsole(lstConsole, "---Это объявление старое")
                    .state = AdvState.old
                    GoTo next_advert
                End If
            End If

            'Загружаем страницу
            response = GetHTTPResponse(oHttpXML, SITE_URL & .url)
            If VarType(response) = VariantType.Long Then
                myConsole(lstConsole, "Сервер недоступен. Код возврата " & response) : Exit Sub
            End If

            'Получаем номер телефона
            regexp = New System.Text.RegularExpressions.Regex("<span id=""estatePhoneSpan"">(.*?)</span>")
            phone = regexp.Match(response).Groups(1).Value.Trim()
            .phone = getPhonesArray(phone)

            'Ищем телефон в риэлторах
            'GoTo skip_phone
            For n = 0 To UBound(.phone)
                If DBEstate_isEstate(.phone(n), True) = True Then
                    myConsole(lstConsole, "---Это риэлтор")
                    .state = AdvState.estateInDB
                    'Сохраняем объявление с риэлтором как старое
                    If DBAdverts_isOldAdvert(ADV_NAME, .advid) = False Then
                        DBAdverts_addOldAdvert(ADV_NAME, .advid)
                    End If
                    'Переходим к следующему объявлению
                    GoTo next_advert
                End If
            Next

skip_phone:
            .state = AdvState.normal

            'Ищем адрес
            regexp = New System.Text.RegularExpressions.Regex("<dd class=""item-content"">(.*)</dd>")
            tmpS = regexp.Match(response).Groups(1).Value
            regexp = New System.Text.RegularExpressions.Regex("<a.*?>(.*?)</a>")
            Dim matches As System.Text.RegularExpressions.MatchCollection
            matches = regexp.Matches(tmpS)
            For n = 3 To matches.Count - 1
                Select Case n
                    Case 3
                        .street = matches(n).Groups(1).Value
                        .street = GetNormStreet(.street, False, {"улица", "проспект", "переулок"})
                    Case 4
                        .nomer = matches(n).Groups(1).Value.Trim()
                End Select
            Next

            'Ищем описание системы
            regexp = New System.Text.RegularExpressions.Regex("<tr class=""info-item"">\s*<td.*?>(.*?)</td>\s*<td.*?>(.*?)</td>")
            matches = regexp.Matches(response)
            For n = 0 To matches.Count - 1
                tmpS2 = matches(n).Groups(1).Value
                If tmpS2 = "Год постройки/ сдачи" Then Continue For
                tmpS = tmpS2 & ": " & matches(n).Groups(2).Value
                tmpS = tmpS.Replace("&nbsp;", " ").Replace("&mdash;", "-")
                regexp = New System.Text.RegularExpressions.Regex("(\(.*?/.*?/.*?\))")
                tmpS = regexp.Replace(tmpS, "").Trim()
                .AddText(tmpS)
                Select Case tmpS2
                    Case "Этаж / этажность"
                        tmpO = matches(n).Groups(2).Value.Replace(" ", "").Split("/")
                        .floor = tmpO(0)
                        .floor_all = tmpO(1)
                        Continue For
                    Case "Материал стен"
                        tmpO = matches(n).Groups(2).Value
                        If tmpO = "кирпич" Then .building = "к"
                        If tmpO = "панель" Then .building = "п"
                        Continue For
                    Case "Кол-во комнат"
                        Continue For
                    Case "Площадь,&nbsp;м<sup>2</sup>"
                        tmpO = matches(n).Groups(2).Value.Replace(" ", "").Replace(",", ".").Split("/")
                        .kvm_all = Replace(CStr(Val(tmpO(0))), ",", ".")
                        .kvm_live = Replace(CStr(Val(tmpO(1))), ",", ".")
                        .kvm_kitchen = Replace(CStr(Val(tmpO(2))), ",", ".")
                        Continue For
                End Select
                .AddContent(tmpS)
            Next

            'Ищем описание клиента
            regexp = New System.Text.RegularExpressions.Regex("<p class=""estate-description"">(.|\n)*?</p>")
            tmpS = regexp.Match(response).Groups(0).Value
            regexp = New System.Text.RegularExpressions.Regex("<p.*?>")
            tmpS = regexp.Replace(tmpS, "")
            tmpS = tmpS.Replace("</p>", "").Trim()
            .AddText(tmpS)
            .AddContent(tmpS)

            'Ищем цену
            regexp = New System.Text.RegularExpressions.Regex("<p class=""price""><strong>(.*)</strong>(.*)</p>")
            matches = regexp.Matches(response)
            tmpS = matches(0).Groups(1).Value
            regexp = New System.Text.RegularExpressions.Regex("\s*")
            tmpS = regexp.Replace(tmpS, "")
            If Val(tmpS) > 99000 And tmpS.EndsWith("000") Then
                tmpS = tmpS.Substring(0, tmpS.Length - 3)
                .AddText(tmpS & "тыс")
            Else
                .AddText(tmpS)
            End If
            .price = tmpS
            .AddText(matches(0).Groups(2).Value.Trim())
            myConsole(lstConsole, "---Объявление загружено")

next_advert:
            lstConsole.Items.Add("")
            If cur_adv = adverts.Count - 1 Then
                'Объявления кончились, выводим их
                grbControl.Enabled = False
                grbGrabber.Enabled = False
                lstConsole.Visible = False
                web.Visible = True
                web.Left = 0
                web.Dock = DockStyle.Bottom
                'web.Navigate("about:blank")
                
                'Подготавливаем список перед выводом
                AdvertsCreateList(adverts, chkSaveNew.Checked, ADV_NAME)

                'Собираем HTML код и обновляем WebBrowser
                AdvertsShowList(web, adverts, h1, AddressOf SpanPhoneClick, ADV_NAME)
            Else
                'Смотрим следующее объявление
                timTick.Interval = 1500
                timTick.Start()
            End If
        End With
        cur_adv = cur_adv + 1
    End Sub



    Private Sub SpanPhoneClick(ByVal sender As Object, ByVal e As System.EventArgs)
        AdvertsSpanPhoneClick(web, adverts, h1, AddressOf SpanPhoneClick, ADV_NAME)
    End Sub



    Public Function getAdverts() As Object
        Return adverts
    End Function
End Class